package com.netty.zhanglong.sixth.example;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

/**
 * Description:
 *
 * @author: StriverFeng
 * @date: 2018-9-24
 * Details:
 */
public class TestClientHandler extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        int radmom = new Random().nextInt(3);
        MyDataInfo.MyMessage myMessage = null;
        switch (radmom) {
            case 0:
                MyDataInfo.Person person = MyDataInfo.Person.newBuilder().setAddress("北京").setAge(20).setName("张三").build();
                myMessage = MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.PersonType).setPerson(person).build();
                break;
            case 1:
                MyDataInfo.Dog dog = MyDataInfo.Dog.newBuilder().setAge(11).setName("dog").build();
                myMessage = MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.DogType).setDog(dog).build();
                break;
            case 2:
                MyDataInfo.Cat cat = MyDataInfo.Cat.newBuilder().setCity("北京").setName("cat").build();
                myMessage = MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.CatType).setCat(cat).build();
                break;
            default:
                break;
        }


        ctx.channel().writeAndFlush(myMessage);
    }
}
